package cn.edu.zjicm.wordsnet_d.data.test;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.activity.ExamReviewActivity;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;

public class ReviewManager{
	public int TEST_INTERVAL = 9;		//学习10个一组
	private int size;

	List<Question> allQuestionList =new LinkedList<Question>();	
	Context mContext;
	WordFactory wf;
	int familiarDegree;
	
	public ReviewManager(Context mContext, int familiarDegree){
		this.mContext = mContext;
		this.familiarDegree = familiarDegree;
		wf = WordFactory.getInstance(mContext);
		getAllQuestions();
	}
	
	void getAllQuestions() {
		List<OneWord> list = null;
		if(familiarDegree == OneWord.DEGREE_UNFAMILIAR) {
			list = wf.getNewWordOrLastForgetWords();
			//Collections.shuffle(list);
			for(int i=0;i<list.size();i++){
				OneWord r = list.get(i);
				allQuestionList.add(new Question(r, mContext));
			}
			size = wf.getNumberOfNewWordOrLastForgetWords();
		} else if(familiarDegree == OneWord.DEGREE_FAMILIAR) {
			list = wf.getFamiliarWordsForReview();
			for(int i=0;i<list.size();i++){
				OneWord r = list.get(i);
				allQuestionList.add(new Question(r, mContext));
			}
			new Thread() {
				public void run() {
					List<OneWord> list = wf.getFamiliarWordsForReview1();	
					
					for(int i=0;i<list.size();i++){
						OneWord r = list.get(i);			
						allQuestionList.add(new Question(r, mContext));
					}	
				};
			}.start();
			size = wf.getFamiliarWordCount();
		}

	}
	
	public Question nextQuestion(){
		if (allQuestionList.size()>0){
			return allQuestionList.get(0);
		}else
			return null;
	}	
	
	public void rightAnswer(Question rQuestion){
		if (allQuestionList.size() > 0)
			allQuestionList.remove(0);
		((ExamReviewActivity)mContext).updateResult();
		
//		if(familiarDegree == OneWord.DEGREE_FAMILIAR)			//如果是熟词的词义回想，更新学习时间；生词的词义回想，不更新学习时间，避免bug#957
//		wf.updateCurrentWordTestTime(rQuestion.getWord());	//更新学习时间
		wf.markCurrentLockedWord(rQuestion.getWord());		//更新锁屏时间		
	}
	
	public void wrongAnswer(Question rQuestion){
		if (allQuestionList.size() > 0)
			allQuestionList.remove(0);		
		if(familiarDegree == OneWord.DEGREE_FAMILIAR){			
			OneWord word = DataManager.getInstance().findWord(rQuestion.getWord(), mContext);
			if(word != null){
				DataManager.getInstance().changeFamiliarToUnFamiliar(word, mContext);
				Toast.makeText(mContext, rQuestion.getLemma() + "已降级为生词", Toast.LENGTH_SHORT).show();
				//wf.markCurrentTestTime(word);
			}	
			((ExamReviewActivity)mContext).updateResult();
		} else {
			addQuestion(TEST_INTERVAL, rQuestion);		//错误测试，间隔8个测试后重新测试
		}
	}
	
	public void setTooTooEasy(Question rQuestion){
		if (allQuestionList.size() > 0)
			allQuestionList.remove(0);
		((ExamReviewActivity)mContext).updateResult();
		
		OneWord word = DataManager.getInstance().findWord(rQuestion.getWord(), mContext);
		if(word != null){
			DataManager.getInstance().changeFamiliarToTooEasy(word, mContext);
			Toast.makeText(mContext, rQuestion.getLemma() + "已设为太简单", Toast.LENGTH_SHORT).show();
		}
	}
	
	void addQuestion(int location, Question question){
		if(location < allQuestionList.size())
			allQuestionList.add(location, question);
		else
			allQuestionList.add(question);
	}
	
	public int size(){
		return size;
	}	
}